package com.example.restaurant.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.restaurant.common.Log;
import com.example.restaurant.common.Result;
import com.example.restaurant.controller.form.AddDishsForm;
import com.example.restaurant.controller.form.UpdateDishByIdForm;
import com.example.restaurant.model.Dish;
import com.example.restaurant.service.DishService;
import com.example.restaurant.service.dto.FeiDishDto;
import com.example.restaurant.service.dto.SelDishTypeByUserId;
import com.example.restaurant.service.dto.SelStoreByUidDto;
import com.example.restaurant.service.param.AddDishsParam;
import com.example.restaurant.service.param.UpdateDishByIdParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 菜品表 前端控制器
 * </p>
 *
 * @author x
 * @since 2022-09-29
 */
@RestController
@Slf4j
public class DishController {
    @Resource
    private DishService dishService;


    @PostMapping("addDishs")
    @Log
    //新增菜品
    public Result addDishs(@RequestBody @Valid AddDishsForm addDishForm){
        log.info("{}",addDishForm.toString());
        AddDishsParam addDishParam = BeanUtil.copyProperties(addDishForm, AddDishsParam.class);
        boolean b = dishService.addDish(addDishParam);
        return Result.success();
    }


    @GetMapping("getDish")
    @Log
    //根据用户id ,分页查询菜品
    public Result getDish(Integer userId,@RequestParam(defaultValue = "1")Integer current,@RequestParam(defaultValue = "5")Integer size){
        log.info("{},{},{}",userId,current,size);
        IPage<FeiDishDto> dish = dishService.getDish(userId, current, size);
        return Result.success(dish);
    }
    @PutMapping("updateDish")
    @Log
    //根据门店id，修改菜品
    public Result updateDish(@RequestBody @Valid UpdateDishByIdForm updateDishByIdForm){
        log.info("{}",updateDishByIdForm.toString());
        UpdateDishByIdParam updateDishByIdParam = BeanUtil.copyProperties(updateDishByIdForm, UpdateDishByIdParam.class);
        boolean b = dishService.updateDish(updateDishByIdParam);
        return Result.success();

    }
    @DeleteMapping("deleteDish")
    @Log
    //根据id，删除菜品
    public Result deleteDish(int dishId){
        log.info("{}",dishId);
        boolean b = dishService.deleteDish(dishId);
        return Result.success();

    }

    //根据菜类ID查询菜品
    @GetMapping("getDishByDishTypeId")
    @Log
    public Result getDishByDishTypeId(int dishTypeId){
        List<Dish> dishes = dishService.getDishByDishTypeId(dishTypeId);
        return Result.success(dishes);
    }
    //预加载新增菜品的下拉框，门店
    @GetMapping("selStoreByUid")
    @Log
    public Result selStoreByUid(int userId) {
        List<SelStoreByUidDto> selStoreByUid = dishService.selStoreByUid(userId);
        return Result.success(selStoreByUid);
    }
    //预加载新增菜品的下拉框，菜类
    @GetMapping("selDishTypeByUserId")
    @Log
    public Result selDishTypeByUserId(){
        Object userId = StpUtil.getExtra("userId");
        String userId1 = String.valueOf(userId);
        int i = Integer.parseInt(userId1);
        List<SelDishTypeByUserId> selDishTypeByUserIds = dishService.selDishTypeByUserId(i);
        return Result.success(selDishTypeByUserIds);
    }
    //根据门店id和模糊菜品名,模糊查询菜品
    @GetMapping("selDishByStoreIdAndName")
    @Log
    public Result selDishByStoreIdAndName(int storeId,String dishName,@RequestParam(defaultValue = "1")Integer current,@RequestParam(defaultValue = "5")Integer size){
        Page<Dish> dishPage = dishService.selDishByStoreIdAndName(storeId, dishName, current, size);
        return Result.success(dishPage);
    }

    //根据门店id和模糊菜品名,模糊查询菜品  带类型
    @GetMapping("getDishByStoreAndName")
    @Log
    public Result getDishByStoreAndName(int storeId,String dishName,@RequestParam(defaultValue = "1")Integer current,@RequestParam(defaultValue = "5")Integer size){
        IPage<FeiDishDto> dishByStoreAndName = dishService.getDishByStoreAndName(storeId, dishName, current, size);
        return Result.success(dishByStoreAndName);
    }


    @GetMapping("getDishByType")
    public Result getDishType(int storeId,int dishTypeId){
        List<Dish> allDishByType = dishService.getAllDishByType(storeId, dishTypeId);
        return Result.success(allDishByType);
    }
    @GetMapping("getDishByName")
    public Result getDishByName(int storeId,String dishName){
        List<Dish> allDishByName = dishService.getAllDishByName(storeId, dishName);
        return Result.success(allDishByName);
    }
    @GetMapping("getAllDish")
    public Result getAllDish(int storeId){
        List<Dish> allDish = dishService.getAllDish(storeId);
        return Result.success(allDish);
    }
}
